/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.DaftarUser;
import model.User;

/**
 *
 * @author dila
 */
public class LoginHelper {

    HttpServletRequest request;

    public LoginHelper(HttpServletRequest request) {
        this.request = request;
    }

    public String login() {
        HttpSession session = request.getSession();
        DaftarUser du = new DaftarUser();
        User user = new User();

        String name = request.getParameter("uname");
        String pass = request.getParameter("pass");

        user.setUsername(name);
        session.setAttribute("temp", user);
        if (name.equals("") || pass.equals("")) {
            session.setAttribute("error", "Masih ada field yang kosong");
            return "index.jsp";
        } else {
            boolean hasilCheck = du.check(name);
            if (hasilCheck) {
                user = null;
                user = du.getUser(name);
                if (name.equals(user.getUsername()) && pass.equals(user.getPass())) {
                    session.setAttribute("uname", user);
                    session.removeAttribute("temp");
                    session.removeAttribute("error");
                    if (user.isLoginStat()) {
                        return "HomeUser.jsp";
                    } else {
                        return "HomeAdmin.jsp";
                    }
                } else {
                    session.setAttribute("error", "gagal login");
                    return "index.jsp";
                }
            } else {
                session.setAttribute("error", "User tidak ditemukan");
                return "index.jsp";
            }
        }
    }
}